package com.dk.controller;

import com.dk.pojo.TabCategory;
import com.dk.pojo.TabRoute;
import com.dk.pojo.TabRouteImg;
import com.dk.pojo.TabSeller;
import com.dk.service.RouteService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("route")

public class RouteController {
    @Autowired
    RouteService routeService;

    @RequestMapping("tolist")
    public String tolsit() {
        return "route/show";

    }

    @RequestMapping("selectserver")
    @ResponseBody
    public Map<String, Object> selectInfo(String rname, Integer rflag, @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "3") Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        PageInfo<TabRoute> page = routeService.selectByname(rname, rflag, pageNumber, pageSize);
        map.put("total", page.getTotal());
        map.put("rows", page.getList());
        return map;

    }

    @RequestMapping("addInfo")
    public String addInfo(Model model) {
        List<TabSeller> seller = routeService.select();
        List<TabCategory> categories = routeService.selectTable();
        model.addAttribute("s", seller);
        model.addAttribute("c", categories);

        return "route/add";

    }

    @RequestMapping("insertinto")
    @ResponseBody
    public boolean insertinto(TabRoute tabRoute) {
        boolean a = routeService.insertinto(tabRoute);
        return a;
    }

    @RequestMapping("huixuan")
    public String get(Integer rid, Model mo) {
        TabRoute a = routeService.selectByrid(rid);
        List<TabSeller> seller = routeService.select();
        List<TabCategory> categories = routeService.selectTable();
        mo.addAttribute("r", a);
        mo.addAttribute("s", seller);
        mo.addAttribute("c", categories);
        return "route/update";
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public boolean updateInfo(TabRoute tabRoute) {
        boolean a = routeService.updateInfo(tabRoute);
        return a;
    }

    @RequestMapping("deleteByid")
    @ResponseBody
    public boolean empdelete(Integer id) {
        boolean a = routeService.deleteByid(id);
        return a;
    }

    @RequestMapping("addimg")
    public String get2(Integer rid, Model mo) {
        mo.addAttribute("rid", rid);

        return "route/addimg";

    }

    @RequestMapping("addimginfo")
    @ResponseBody
    public boolean addimginfo(TabRouteImg t) {
        String bigpic = t.getBigpic();
        String[] split = bigpic.split(",");
        boolean a = routeService.addimginfo(split, t);
        return a;
    }
}
